home *** CD-ROM | disk | FTP | other *** search
/ Software Explosion / Software Explosion (Fore-Matt Home Computing)(1996).iso / system / c / lowfrag.e < prev    next >
Text File  |  1996-01-12  |  2KB  |  94 lines

  1. /* LowFrag - patches AllocMem() and AllocVec(), trying to ease up
  2.    memory fragmentation - version 1.2 */
  3.  
  4. /* Future ideas:
  5.  
  6.       Task filter list?
  7.       Promote "MEMF_PUBLIC" allocations?
  8.  
  9.    -Safer removal (check if no-one patched over me before removing).
  10. */
  11.  
  12. MODULE 'exec/memory','dos/dos'
  13.  
  14.  
  15. PROC main()
  16. DEF oldfalloc,oldfallocvec
  17. DEF limit
  18.  
  19.     IF StrLen(arg) >0
  20.        limit:=Val(arg)
  21.        IF (limit <10) OR (limit > 1000000)
  22.           WriteF('Invalid value!\n')
  23.           CleanUp(20)
  24.        ENDIF
  25.        PutLong({threshold},limit)
  26.     ENDIF
  27.  
  28.     Forbid()     -> Protect our back.
  29.  
  30. -> Patch AllocMem()...
  31.  
  32.     IF oldfalloc:=SetFunction(execbase, $ff28, {newfalloc})
  33.        PutLong({patchalloc}, oldfalloc)
  34.  
  35. -> ...and AllocVec().
  36.  
  37.        IF oldfallocvec:=SetFunction(execbase, $fd54, {newfallocvec})
  38.           PutLong({patchallocvec}, oldfallocvec)
  39.  
  40. -> Return to normal.
  41.           Permit()
  42.  
  43. -> Wait for CTRL-C.
  44.           Wait(SIGBREAKF_CTRL_C)
  45.  
  46. -> Restoring the original vectors.   NOTE: Not safe - should check if no one
  47. -> has done a SetFunction() over me!
  48.  
  49.           Forbid()
  50.           SetFunction(execbase, $fd54, oldfallocvec)
  51.        ENDIF
  52.        SetFunction(execbase, $ff28, oldfalloc)
  53.     ENDIF
  54.  
  55.     Permit()
  56.  
  57. ENDPROC
  58.  
  59.  
  60. -> Storage for the threshold value
  61. -> Default to 32 Kb.  This value seemed to be pretty good on my system.
  62. threshold:
  63. LONG 32768
  64.  
  65. -> Storage for the original vectors
  66. patchalloc:
  67. LONG 0
  68.  
  69. patchallocvec:
  70. LONG 0
  71.  
  72.  
  73. /* The new code, which will "promote" allocations when needed, before
  74.    returning to the real AllocMem/AllocVec code. */
  75.  
  76. newfalloc:
  77.   CMP.L threshold(PC), D0
  78.   BGE exit2
  79.   OR.L #MEMF_REVERSE, D1
  80. exit2:
  81.   MOVE.L patchalloc(PC), -(A7)
  82.   RTS
  83.  
  84. newfallocvec:
  85.   CMP.L threshold(PC), D0
  86.   BGE exit1
  87.   OR.L #MEMF_REVERSE, D1
  88. exit1:
  89.   MOVE.L patchallocvec(PC), -(A7)
  90.   RTS
  91.  
  92.  
  93. CHAR '$VER:lowfrag 1.2 (1.2.96) By Eric Sauvageau.',0
  94.